package com.cuitrip.business.login;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cuitrip.app.UserConfig;
import com.cuitrip.app.rong.RongCloudEvent;
import com.cuitrip.business.user.model.CtUserInfo;
import com.lab.rtenvironment.AppCoreRuntime;
import com.lab.utils.LogHelper;

public class LoginInstance {


    static final String KEY_USERINFO_ID = "_user_info";

    private static LoginInstance sInstance;
    private CtUserInfo sCtUserInfo;

    private LoginInstance() {
    }

    public synchronized static LoginInstance getInstance() {
        if (sInstance == null) {
            sInstance = new LoginInstance();
            sInstance.read();
        }
        return sInstance;
    }

    private void read() {
        SharedPreferences sp = AppCoreRuntime.context.getSharedPreferences(KEY_USERINFO_ID, Context.MODE_PRIVATE);
        String userInfo = sp.getString(KEY_USERINFO_ID, null);
        if (userInfo != null) {
            sCtUserInfo = JSON.parseObject(userInfo, CtUserInfo.class);
        }
    }



    public static void update(Context context, CtUserInfo ctUserInfo, boolean reconnectRongyun) {
        LoginInstance info = getInstance();
        if (info.sCtUserInfo != null && !TextUtils.isEmpty(info.sCtUserInfo.getUid()) && ctUserInfo != null && !TextUtils.isEmpty(ctUserInfo.getUid()) && info.sCtUserInfo.getUid().equals(ctUserInfo.getUid())) {
            if (TextUtils.isEmpty(ctUserInfo.getRongyunToken())) {
                ctUserInfo.setRongyunToken(info.sCtUserInfo.getRongyunToken());
            }
        }
        info.sCtUserInfo = ctUserInfo;
        SharedPreferences sp = context.getSharedPreferences(KEY_USERINFO_ID, Context.MODE_PRIVATE);
        if (ctUserInfo != null) {
            LogHelper.e("omg", "save user info  :" + JSONObject.toJSON(ctUserInfo).toString());

            sp.edit().putString(KEY_USERINFO_ID, JSONObject.toJSON(ctUserInfo).toString())
                    .commit();
        } else {
            sp.edit().remove(KEY_USERINFO_ID).commit();
            UserConfig.clear();
        }
        LogHelper.e("omg", "update ed ");
        if (reconnectRongyun){
            RongCloudEvent.ConnectRongForce();
            LogHelper.e("omg", "ConnectRongForce ");
        }
    }

    public static void logout(Context context) {
        UserConfig.clear();
        SharedPreferences sp = context.getSharedPreferences(KEY_USERINFO_ID, Context.MODE_PRIVATE);
        sp.edit().remove(KEY_USERINFO_ID).commit();
        LoginInstance info = getInstance();
        info.sCtUserInfo = null;

    }

    public static boolean isLogin() {
        CtUserInfo info = getInstance().sCtUserInfo;
        if (info == null) {
            return false;
        }
        return !TextUtils.isEmpty(info.getToken());
    }

    public CtUserInfo getUserInfo() {
        return sCtUserInfo;
    }


    public static CtUserInfo updateProfile(Context context, CtUserInfo mCtUserInfo, boolean reconnectRongyun) {
        CtUserInfo user = LoginInstance.getInstance().getUserInfo();
        if (user==null){
            return null;
        }
        CtUserInfo.copyFrom(user, mCtUserInfo);
        update(context, user, reconnectRongyun);
        return user;
    }
}
